<template>
  <div class="header">
    <slot name="header">
      <div class="title">{{ title }}</div>

      <div class="headler">
        <slot name="header2"></slot>
      </div>
    </slot>
  </div>

  <el-table
    :data="listData"
    border
    style="width: 100%"
    @selection-change="selectionChange"
    row-key="id"
    :tree-props="{ children: 'children' }"
    :v-bind="childrenProps"
  >
    <!-- 选中的栏 -->
    <el-table-column
      v-if="showSelectColumn"
      type="selection"
      align="center"
      width="60"
    >
    </el-table-column>

    <!-- 添加序号 -->
    <el-table-column
      v-if="showIndexColumn"
      type="index"
      label="序号"
      align="center"
      width="60"
    >
    </el-table-column>

    <!-- //content -->
    <template v-for="item in propList" :key="item.prop">
      <el-table-column
        v-bind="item"
        align="center"
        size="mini"
        show-overflow-tooltip
      >
        <template #default="scope">
          <slot :name="item.slotName" :row="scope.row">{{
            scope.row[item.prop]
          }}</slot>
        </template>
      </el-table-column>
    </template>
  </el-table>

  <!-- 页脚 -->
  <div class="footer" v-if="showFooter">
    <slot name="footer">
      <!-- //分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.currentPage"
        :page-size="page.pageSize"
        :page-sizes="[10, 20, 30]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="listCount"
      >
      </el-pagination>
    </slot>
  </div>
</template>

<script lang="ts">
import { defineComponent } from "vue";

export default defineComponent({
  props: {
    //标题
    title: {
      type: String,
      default: () => [],
    },
    listData: {
      type: Array,
      default: () => [],
    },
    showIndexColumn: {
      type: Boolean,
      default: () => false,
    },
    showSelectColumn: {
      type: Boolean,
      default: () => false,
    },
    //分页器显示的个数
    listCount: {
      type: Number,
      default: 0,
    },
    propList: {
      type: Array,
    },
    page: {
      type: Object,
      default: () => ({ currentPage: 0, pageSize: 10 }),
    },
    childrenProps: {
      type: Object,
      default: () => ({}),
    },
    showFooter: {
      type: Boolean,
      default: true,
    },
  },
  emits: ["selectionChange", "update:page"],
  setup(props, { emit }) {
    const selectionChange = (value: any) => {
      emit("selectionChange", value);
    };
    const handleCurrentChange = (currentPage: number) => {
      emit("update:page", { ...props.page, currentPage });
    };
    const handleSizeChange = (pageSize: number) => {
      emit("update:page", { ...props.page, pageSize });
    };

    return { selectionChange, handleSizeChange, handleCurrentChange };
  },
});
</script>

<style scoped lang="less">
.header {
  display: flex;
  justify-content: space-between;
  .title {
    color: #606266;
    font-size: 16px;
    font-weight: 700;
    padding-top: 10px;
  }
  .headler {
    padding-bottom: 10px;
  }
}
.footer {
  margin-top: 15px;
  text-align: right;
}
</style>
